﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Net; 
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;

using System.IO;
using System.Reflection;

namespace Wesley.Crawler.SimpleCrawler
{
    class Program
    {
        static   void Main(string[] args)
        {
			var key = args[0];
			ICrawler crawler = new AQiyiCrawler();
			var url = string.Format("http://so.iqiyi.com/so/q_{0}?source=input&sr={1}", key, DateTime.Now.ToString("yyyyMMddffff"));
		   var task= 	crawler.Start(new Uri(url));

			  Task.WaitAll(task);
			var result = task.Result;


			SaveJson(key, result);
			 
			 
		}

		private static void SaveJson(string key,object data) {
			var fileCurrentDirectory = System.Environment.CurrentDirectory + "\\Videos";
			var filePath = fileCurrentDirectory + "\\" + key + ".json";
			try
			{
				 ;
				Directory.CreateDirectory(fileCurrentDirectory);

				File.Delete(filePath);
			 
			}
			catch (Exception)
			{

				 
			}
			 
			File.AppendAllText(filePath  , Newtonsoft.Json.JsonConvert.SerializeObject(data));

		}

		 
    }








   



}


